﻿using Engine;
using Engine.Graphics;
using MFTrain;

namespace Game
{
    public class MFWaterWidget : CanvasWidget
    {
        public bool m_textureWrap;

        public bool m_textureLinearFilter;

        public bool m_depthWriteEnabled;

        private float value;

        public MFScreenSpaceSteamRenderer steamRenderer = new MFScreenSpaceSteamRenderer(100);

        public Texture2D Texture { get; protected set; }

        public Color Color { get; set; }

        public float Value
        {
            get
            {
                return value;
            }
            set
            {
                this.value = MathUtils.Clamp(value, 0f, 1f);
            }
        }

        public float WaterScale { get; set; }

        public float ParticlesPerSecond
        {
            get
            {
                return steamRenderer.ParticlesPerSecond;
            }
            set
            {
                steamRenderer.ParticlesPerSecond = value;
            }
        }

        public MFWaterWidget()
        {
            base.Size = new Vector2(float.PositiveInfinity);
            base.ClampToBounds = true;
            Color = Color.Black;
            Texture = ContentManager.Get<Texture2D>("MF/Train/Water");
        }

        public override void Draw(DrawContext dc)
        {
            steamRenderer.Draw(dc.PrimitivesRenderer2D, 0f, base.GlobalTransform, base.GlobalColorTransform);
            Matrix m = base.GlobalTransform;
            Vector2 v = Vector2.Zero;
            Vector2 v2 = new Vector2(base.ActualSize.X, 0f);
            Vector2 v3 = base.ActualSize;
            Vector2 v4 = new Vector2(0f, base.ActualSize.Y);
            v += new Vector2(0f, (1f - value) / 1f * base.Size.Y);
            v2 += new Vector2(0f, (1f - value) / 1f * base.Size.Y);
            Vector2.Transform(ref v, ref m, out var result);
            Vector2.Transform(ref v2, ref m, out var result2);
            Vector2.Transform(ref v3, ref m, out var result3);
            Vector2.Transform(ref v4, ref m, out var result4);
            Color color = Color * base.GlobalColorTransform;
            if (color.A != 0)
            {
                TexturedBatch2D texturedBatch2D = dc.PrimitivesRenderer2D.TexturedBatch(Texture, useAlphaTest: true, 1, DepthStencilState.None, null, null, SamplerState.PointWrap);
                Vector2 zero = Vector2.Zero;
                float num = 1f / 3f * WaterScale;
                float num2 = base.ActualSize.Y * value / base.ActualSize.X;
                Vector2 texCoord = zero;
                Vector2 texCoord2 = zero + Vector2.UnitX * num;
                Vector2 texCoord3 = zero + Vector2.One * new Vector2(num, num * num2);
                Vector2 texCoord4 = zero + Vector2.UnitY * num * num2;
                texturedBatch2D.QueueQuad(result, result2, result3, result4, 0f, texCoord, texCoord2, texCoord3, texCoord4, color);
            }
        }

        public override void Update()
        {
            float dt = MathUtils.Clamp(Time.FrameDuration, 0f, 0.1f);
            steamRenderer.Origin = new Vector2(0f, (1f - value) / 1f * base.Size.Y);
            steamRenderer.CutoffPosition = float.NegativeInfinity;
            steamRenderer.ParticleSize = 32f;
            steamRenderer.ParticleSpeed = 32f;
            steamRenderer.Width = base.ActualSize.X;
            steamRenderer.MinTimeToLive = 0.5f;
            steamRenderer.MaxTimeToLive = 2f;
            steamRenderer.ParticleAnimationPeriod = 1.25f;
            steamRenderer.Update(dt);
        }

        public override void MeasureOverride(Vector2 parentAvailableSize)
        {
            base.IsDrawRequired = true;
            base.DesiredSize = base.Size;
        }
    }
}